var getRating = function(ownerId){
	this.ownerId = ownerId;
	this.error = options.error;
	this.success = options.success;
        this.getRating();
};

getRating.prototype._success = function(data){
	if(typeof this.success == 'function') {
		this.success(data);
	}
};

getRating.prototype._error = function(code, message){
	if(typeof this.error == 'function') {
		this.error(code, message);
	}
};

getRating.prototype.check = function(nextStatement){
	var self = this;
	Mysql.query("SELECT COUNT(*) AS c1 FROM rating WHERE accountId="+Mysql.escape(self.ownerId)+" LIMIT 1", function(error, results){
		if(error) {
			console.log('Mysql error: '+error);
			self._error(9999, 'Mysql error');
			return;
		}
		if(results[0].c1 == 0) {
			self._error(9999, 'Not found account');
			return;
		}
		nextStatement();
	});
};

getRating.prototype.getRating = function(){
	var self = this;
	self.check(function(){
		Mysql.query("SELECT ratinggroup.id,rating.accountId,rating.ratingGroup,ratinggroup.ratingAll,ratinggroup.rating1,ratinggroup.rating2,ratinggroup.rating3,ratinggroup.rating4,ratinggroup.rating5 FROM ratinggroup JOIN rating ON ratinggroup.id=rating.ratingGroup WHERE rating.accountId="+Mysql.escape(self.ownerId), function(error, results){
			if(error) {
				console.log('Mysql error: '+error);
				self._error(9999, 'Mysql error');
				return;
			}
                        var rating_avg = (results.rating1 + results.rating2 + results.rating3 + results.rating4 + results.rating5) / 5 
			self._success({ id:results.id, ratingAll:results.ratingAll, rating1:results.rating1, rating2:results.rating2, rating3:results.rating3, rating4:results.rating4, rating5:results.rating5, rating:rating_avg });
		});
	});
};

module.exports = getRating;